<?php
namespace Admin\Controller;
use Think\Controller;
class CategoryController extends BaseController
{
    public function add()
    {
        $model = D('Category');
    	if(IS_POST)
    	{
    		$model = D('Category');
    		if($model->create(I('post.'), 1))
    		{
    			if($id = $model->add())
    			{
    				$this->success('添加成功！', U('lst'));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
        //取出所有分类做下拉框
        $cateGory = $model->getTree();
		// 设置页面中的信息
		$this->assign(array(
		    'cateGory' => $cateGory,
			'_page_title' => '添加分类',
			'_page_btn_name' => '添加商品分类',
			'_page_btn_link' => U('lst'),
		));
		$this->display();
    }
    public function edit()
    {
        $model = D('Category');
    	$id = I('get.id');
    	if(IS_POST)
    	{
    		if($model->create(I('post.'), 2))
    		{
    			if($model->save() !== FALSE)
    			{
    				$this->success('修改成功！', U('lst'));
    				exit;
    			}
    		}
    		$this->error($model->getError());
    	}
        $cateGory = $model->getTree();
    	$data = $model->find($id);
        $children = $model->getChildren($id);
        $this->assign(array(
            'cateGory' => $cateGory,
            'data' => $data,
            'children' => $children
        ));
		// 设置页面中的信息
		$this->assign(array(
			'_page_title' => '分类修改',
			'_page_btn_name' => '修改分类列表',
			'_page_btn_link' => U('lst'),
		));
		$this->display();
    }
    public function delete()
    {
    	$model = D('Category');
    	if($model->delete(I('get.id')) !== FALSE)
    	{
    		$this->success('删除成功！', U('lst'));
    		exit;
    	}
    	else 
    	{
    		$this->error("删除失败 原因：" .$model->getError());
    	}
    }
    public function lst()
    {
    	$model = D('Category');
    	$data = $model->getTree();
    	/*$this->assign(array(
    		'data' => $data['data'],
    		'page' => $data['page'],
    	));*/

		// 设置页面中的信息
		$this->assign(array(
            'data' => $data,
			'_page_title' => '分类列表',
			'_page_btn_name' => '添加新分类',
			'_page_btn_link' => U('add'),
		));
    	$this->display();
    }
}